package com.xingzhe.pengji.security.controller.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xingzhe.pengji.framework.controller.PaginationController;
import com.xingzhe.pengji.security.domain.Authorite;
import com.xingzhe.pengji.security.services.AuthoriteService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 */
@Controller
@RequestMapping("/authorite")
public class AuthoriteController extends PaginationController
{

    private Authorite authorite;
    private List<Authorite> authoritesList;
    private ModelAndView modelAndView;
    @Autowired
    private AuthoriteService<Authorite> authoriteService;
    private String operatorManString = "Admin";
    private final String GROUP_INDEX = "redirect:/authorite/index.html";

    //private final static Logger logger = LoggerFactory.getLogger(AuthoriteController.class);

    @Override
    protected ModelAndView executePaginationController(HttpServletRequest request, HttpServletResponse response,
            int pageNumber, int pageSize) throws Exception
    {

        authoritesList = authoriteService.queryForPageAuthoriteList(null, "id", pageSize, pageNumber, false);
        modelAndView = new ModelAndView();
        modelAndView.addObject("authoritesList", authoritesList);
        modelAndView.setViewName("/jsp/authorite/queryPortalAuthorite");

        return modelAndView;
    }

    @Override
    protected int getItemCount(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return authoriteService.getItemCount(authorite);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addAuthorite()
    {
        return "/jsp/authorite/addAuthorite";
    }

    @RequestMapping(value = "/save.html")
    public String saveAuthorite(@ModelAttribute("authorite")
    Authorite authorite)
    {
        authorite.setCreateTime(new Date());
        authorite.setCreator(operatorManString);
        authorite.setUpdateTime(new Date());
        authorite.setUpdateMan(operatorManString);
        authoriteService.saveAuthorite(authorite);
        return GROUP_INDEX;
    }

    @RequestMapping(value = "/saveOrUpdate.html")
    //, BindingResult result
    public String saveOrUpdateAuthorite(@ModelAttribute("authorite")
    Authorite authorite)
    {
        authorite.setUpdateTime(new Date());
        authorite.setUpdateMan(operatorManString);
        authoriteService.updateAuthorite(authorite);
        return GROUP_INDEX;
    }

    @RequestMapping(value = "/update.html")
    public ModelAndView updateAuthorite(@RequestParam("authoriteIds")
    String authoriteIds)
    {

        String[] stringargs = authoriteIds.split(",");

        authorite = new Authorite();
        authorite.setId(Integer.valueOf(stringargs[0]));
        authorite = authoriteService.getAuthoriteById(authorite);
        modelAndView = new ModelAndView();
        modelAndView.addObject("authorite", authorite);
        modelAndView.setViewName("/jsp/authorite/updateAuthorite");

        return modelAndView;
    }

    @RequestMapping(value = "/delete.html")
    public String delAuthorite(@RequestParam("authoriteIds")
    String authoriteIds)
    {

        String[] argsString = authoriteIds.split(",");
        authorite = new Authorite();
        for ( String string : argsString )
        {
            authorite.setId(Integer.valueOf(string));
            authorite.setUpdateTime(new Date());
            authoriteService.delAuthorite(authorite);
        }
        return GROUP_INDEX;
    }

    @ResponseBody
    @RequestMapping(value = "/validateName.html")
    public Object validateAccount(@RequestParam("name")
    String name)
    {
        authorite = new Authorite();
        authorite.setName(name);
        authorite = authoriteService.getAuthoriteByName(authorite);
        if ( null == authorite )
        {
            return true;
        }
        return false;
    }
}
